package com.sw.small_world.fragment.me.myinfo;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.ClickButtonActivity;
import com.sw.small_world.fragment.base.BaseFragment;
import com.sw.small_world.utils.BitmapUtils;
import com.sw.small_world.utils.CommonUtils;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.DialogUtils;
import com.sw.small_world.utils.FileUtils;
import com.sw.small_world.utils.ToastUtil;
import com.sw.small_world.widgets.CircleImageView;
import com.sw.small_world.window.SelectImgMethodPopWindow;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;
import shanyao.pickview.OptionsPickerView;
import shanyao.pickview.utils.PickViewUtils;

/**
 * Created by Administrator on 2017/3/16.
 */

public class EditMyInfoFragment extends BaseFragment {

    @Bind(R.id.iv_portrait)
    CircleImageView ivPortrait;
    @Bind(R.id.ll_portrait)
    LinearLayout llPortrait;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_tele_num)
    EditText etTeleNum;
    @Bind(R.id.et_company)
    EditText etCompany;
    @Bind(R.id.et_field)
    TextView etField;
    @Bind(R.id.et_position)
    TextView etPosition;
    @Bind(R.id.et_address)
    EditText etAddress;
    @Bind(R.id.et_more_address)
    EditText etMoreAddress;
    private ClickButtonActivity activity;
    private SelectImgMethodPopWindow mSelectImgMethodPopWindow;
    private Bitmap mBitmap;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (ClickButtonActivity) getActivity();
    }

    @Override
    protected Object requestData() {
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View getSuccessView() {
        View view = View.inflate(activity, R.layout.fragment_edit_my_info, null);
        ButterKnife.bind(this,view);
        return view;
    }

    @OnClick({R.id.ll_portrait, R.id.et_field, R.id.et_position})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_portrait:
                //创建下方的弹出窗口并且显示出来
                mSelectImgMethodPopWindow = new SelectImgMethodPopWindow(activity, this);
                mSelectImgMethodPopWindow.showAsDropDown(activity.line); //设置layout在PopupWindow中显示的位置
                break;
            case R.id.et_field:
                final ArrayList<String> fieldList = new ArrayList<>();
                fieldList.add("杀猪");
                fieldList.add("卖肉");
                fieldList.add("IT");
                PickViewUtils.initOptionPickView(activity, fieldList, "请选择行业", new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3) {
                        etField.setText(fieldList.get(options1));
                    }
                }).show();
                break;
            case R.id.et_position:
                final ArrayList<String> positionList = new ArrayList<>();
                positionList.add("产品");
                positionList.add("UI");
                positionList.add("安卓");
                positionList.add("ios");
                positionList.add("java");
                PickViewUtils.initOptionPickView(activity, positionList, "请选择职业", new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3) {
                        etPosition.setText(positionList.get(options1));
                    }
                }).show();
                break;
            //点击了弹出窗口的相册中选择
            case R.id.tv_select_img_album:
                //调用android的图库
                Intent toLocalAlbum = new Intent(Intent.ACTION_PICK, null);
                toLocalAlbum.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                this.startActivityForResult(toLocalAlbum, ConstantUtils.ACTIVITY_ALBUM_REQUEST_CODE);
                break;
            //点击pop窗口传递上来的监听弹出窗口的拍照
            case R.id.tv_select_img_take_photo:
                RxPermissions.getInstance(activity)
                        .request(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Action1<Boolean>() {
                            @Override
                            public void call(Boolean aBoolean) {
                                if(aBoolean){
                                    //传入路径直接传入本机路径 内部方法判断sd卡是否正常,所以此处只要判断相机功能是否ok
                                    if(CommonUtils.isCameraCanUse()){
                                        Intent toCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                        //内部做了sd卡判断
                                        Uri imageUri = Uri.fromFile(FileUtils.getHeadPhotoCameraFile());
                                        //给intent传入两个参数
                                        toCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                                        toCameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                                        //开启调用相机模式
                                        EditMyInfoFragment.this.startActivityForResult(toCameraIntent, ConstantUtils.ACTIVITY_CAMERA_REQUEST_CODE);
                                    }
                                } else{
                                    DialogUtils.showPermissionDialog(activity,R.string.permission_camera,EditMyInfoFragment.this);
                                }
                            }
                        });
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //无论做什么操作,返回的时候这个窗口都要消失
        int i = 0;
        i++;
        if(mSelectImgMethodPopWindow!=null){
            this.mSelectImgMethodPopWindow.dismiss();
        }
        switch (requestCode) {
            //如果返回的是打开相册的代号
            case ConstantUtils.ACTIVITY_ALBUM_REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) {
                    //如果中途按了取消,则data为空,那么为了防止报错,需要手动return.
                    if(data==null){
                        return;
                    }
                    if (data.getData() == null) {
                        ToastUtil.show("获得失败");
                        return;
                    }
                    //将图片直接丢入裁剪的intent里面进行操作
                    BitmapUtils.cutPhoto(this, data.getData(), true);
                }
                break;
            //如果返回的是打开了照相机的代号
            case ConstantUtils.ACTIVITY_CAMERA_REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) {
                    //调用裁剪
                    BitmapUtils.cutPhoto(this, Uri.fromFile(FileUtils.getHeadPhotoCameraFile()), true);
                }
                break;
            //如果是裁剪
            case ConstantUtils.ACTIVITY_CUT_IMAGE_REQUEST_CODE:
                //如果中途按了取消,则data为空,那么为了防止报错,需要手动return.
                if(data==null){
                    return;
                }
                String path = FileUtils.getHeadPhotoCutPath();
                mBitmap = BitmapFactory.decodeFile(path);
                this.ivPortrait.setImageBitmap(mBitmap);
//                //最后将头像修改状态改为true
//                isChangeIcon = true;
                break;
            case ConstantUtils.CODE_PERMISSION:
                mSelectImgMethodPopWindow.dismiss();
                break;
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
